<template>
  <section class="component verify-file-upload">
    <el-form :model="model" :rules="rules" ref="form" label-width="90px">
      <el-form-item label="上传附件" prop="fileIds">
        <file-upload :fileList.sync="fileList" ></file-upload>
      </el-form-item>
      <el-form-item label="说明" prop="description">
        <el-input type="textarea" v-model="model.description" :maxlength="500" show-word-limit></el-input>
      </el-form-item>
    </el-form>
    <div class="operate-buttons">
      <el-button @click="close">取消</el-button>
      <el-button @click="commit">确定</el-button>
    </div>
  </section>
</template>

<script lang="ts">
import Vue from "vue";
import Component from "vue-class-component";
import { Dependencies } from "~/core/decorator";
import { Emit, Prop, Watch } from "vue-property-decorator";
import { Form } from "element-ui";
import FileUpload from "~/components/common/file-upload.vue"
import { CaseVerifyService } from "~/services/domain-service/case-verify.service";

@Component({
  components: {
    FileUpload
  }
})
export default class VerifyFileUpload extends Vue {
  @Dependencies(CaseVerifyService)
  private caseVerifyService: CaseVerifyService;

  @Prop({ required: true })
  private rowData;

  private fileList: any[] = [];

  private model: any = {
    fileIds: "",
    description: ""
  }

  private rules = {
    fileIds: { required: true, message: "请上传附件" },
    description: { required: true, message: "请输入说明" }
  }

  @Watch("fileList", {deep: true})
  private watchFileList() {
    this.model.fileIds = this.fileList.length === 0 ? "" : this.fileList.map(v => v.response.id).join(",")
  }

  @Emit("refreshData")
  private refreshData(){}
  @Emit("close")
  private close() {
    const form = this.$refs['form'] as Form
    form.resetFields()
    this.fileList = []
  }

  private commit() {
    const form = this.$refs["form"] as Form
    form.validate((v) => {
      if (!v) return
      this.caseVerifyService.uploadCaseVerifyFile({...this.model, caseId: this.rowData.id}).subscribe(
        data => {
          this.$message.success("上传成功")
          this.refreshData()
          this.close()
        }
      )
    })

  }


}
</script>

<style lang="less" scoped>
</style>
